package top.codedance.iotp.loadblance;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.entity.NetworkEntity;
import top.codedance.iotp.common.entity.RegistServerMeta;
import top.codedance.iotp.common.enums.HttpStatus;
import top.codedance.iotp.common.util.HttpRequestUtil;
import top.codedance.iotp.loadblance.algorithm.Adapter;
import top.codedance.iotp.loadblance.calgorithm.CAdapter;
import top.codedance.iotp.loadblance.common.AlgorithmType;
import top.codedance.iotp.loadblance.common.CAlgorithmType;
import top.codedance.iotp.loadblance.entiy.RegistyUri;

import java.util.ArrayList;
import java.util.List;

public class IotClientLoadBlance extends IotServerLoadBlance {

    private static IotClientLoadBlance self = null;
    private CAlgorithmType cat = CAlgorithmType.AUTO;

    protected IotClientLoadBlance(List<RegistyUri> registryUrls) {
        super(registryUrls);
    }

    protected IotClientLoadBlance(List<RegistyUri> registryUrls, AlgorithmType at, CAlgorithmType cat) {
        super(registryUrls, at);
        this.cat = cat;
    }

    public static IotClientLoadBlance getInstance(List<RegistyUri> registryUrls, AlgorithmType at, CAlgorithmType cat){
        if(self == null){
            synchronized (IotClientLoadBlance.class){
                if(self == null){
                    self = new IotClientLoadBlance(registryUrls, at, cat);
                }
            }
        }
        return self;
    }

    /**
     * 负载均衡
     * @param deviceId
     * @return
     */
    public String blanceHttp(String deviceId){
        try {
            Adapter adapter = at.getAlgorithm().newInstance();
            return adapter.execute(registryUrls, (registryUrl) -> {
                String result = null;
                JSONObject jsonObject = new JSONObject();
                JSONObject body = HttpRequestUtil.post(registryUrl + "/rest/api/query/server/list", jsonObject.toJSONString());
                if (body != null && body.getIntValue("status") == HttpStatus.OK.getCode()) {
                    JSONArray data = body.getJSONArray("data");
                    if(data.size() > 0){
                        try{
                            CAdapter cAdapter = cat.getAlgorithm().newInstance();
                            List<RegistServerMeta> rsms = new ArrayList<>();
                            JSONObject jo;
                            for(int i = 0;i < data.size(); i++){
                                jo = data.getJSONObject(i);
                                rsms.add(RegistServerMeta.parse(jo.toJSONString()));
                            }
                            NetworkEntity execute = cAdapter.execute(rsms);
                            result = execute.getIp() + ":" + execute.getPort();
                        }catch (Exception e){}
                    }
                }
                return result;
            });
        }catch (Exception ex){
            return null;
        }
    }
}
